//
//  PlayHelper.m
//  Kugou-OC
//
//  Created by BH on 16/5/10.
//  Copyright © 2016年 Sunny. All rights reserved.
//

#import "PlayHelper.h"
#import "MusicInfoModel.h"
#import "NetworkHelper.h"
#import "GlobalDefine.h"

@interface PlayHelper ()

@end

@implementation PlayHelper

+(instancetype)sharePlayHelper{
    static PlayHelper *player = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        player = [[self alloc] init];
    });
    return player;
}

- (instancetype)init {
    if (self = [super init]) {
        self.aPlayer = [[AVQueuePlayer alloc] init];
        self.playMode = kPlayModeSerious;
    }
    AVAudioSession *session = [AVAudioSession sharedInstance];
    [session setCategory:AVAudioSessionCategorySoloAmbient error:nil];
    [session setActive:YES error:nil];
    return self;
}

//获取本地音乐
-(NSArray *)getLocalMusic{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"LocalMusicList" ofType:@"plist"];
    NSDictionary *localMusicDict = [NSDictionary dictionaryWithContentsOfFile:path];
    
    NSMutableArray *localMusicArr = [NSMutableArray array];
    for (NSString *key in localMusicDict.keyEnumerator) {
        MusicInfoModel *model = [[MusicInfoModel alloc] init];
        NSArray *tempArr = [key componentsSeparatedByString:@"-"];
        model.music_name = [tempArr lastObject];
        model.music_artist = [tempArr firstObject];
        model.music_type = localMusicDict[key];
        model.music_extname = @"mp3";
        model.music_source = kMusicSourceLocal;
        [localMusicArr addObject:model];
    }
    
    return localMusicArr;
}

/**
 *  获取缓存路径
 *
 *  @param name 文件名
 *
 *  @return 缓存路径
 */
- (NSString*)getMusicPath:(NSString *)name {
    // 获取Caches目录路径
    NSLog(@"%@",CACHE_DIRECTORY);
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *finalPath = [CACHE_DIRECTORY stringByAppendingPathComponent:name];
    // Remove the filename and create the remaining path
    [fileManager createDirectoryAtPath:[finalPath stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:nil];
    
    return finalPath;
}

-(void)getMusicUrlWithParam:(NSDictionary *)param method:(NSString *)method success:(SuccessBlock)success failure:(FailureBlock)failure{
    
    NSString *url = @"http://m.kugou.com/app/i/getSongInfo.php";
    if ([method isEqualToString:@"sync"]) {
        [[NetworkHelper shareNetwork] syncGetDataWithUrl:url params:param view:nil success:^(id response) {
            if (success) {
                success(response);
            }
        } failure:^(NSError *error) {
            
        }];
    }else{
        [[NetworkHelper shareNetwork] getDataWithUrl:url params:param view:nil success:^(id response) {
            if (success) {
                success(response);
            }
        } failure:^(NSError *error) {
            
        }];
    }
}

-(void)downloadMusicWithUrl:(NSString *)url method:(NSString *)method param:(NSDictionary *)param filename:(NSString *)filename  success:(SuccessBlock)success failure:(FailureBlock)failure{
    
    if ([method isEqualToString:@"sync"]) {
        //同步
        [[NetworkHelper shareNetwork] syncGetDataWithUrl:url params:nil view:nil success:^(id response) {
            
            if (success) {
                [self getMusicPath:filename];
                success(response);
            }
            
        } failure:^(NSError *error) {
            
        }];
    }else{
        [[NetworkHelper shareNetwork] downloadWithUrl:url saveToPath:[self getMusicPath:filename] progress:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
            //        NSLog(@"%lld",totalBytesRead);
            //缓存进度
            
        } success:^(id response) {
            
            if (success) {
                success(response);
            }
            
        } failure:^(NSError *error) {
            NSLog(@"%@",error.localizedDescription);
        }];
    }
}

@end
